﻿using System;
using FwtXnaGame3.Common;
using GpgCore.CityItems;
using GpgCore.Corporations;
using GpgCore.Government;
using GpgCore.MapItems;
using GpgCore.Maps;
using GpgCore.Market;
using GpgCore.Products;

namespace GpgCore.GameWorld
{
    public class City : MapItem
    {
        public string Name;
        public Citizens Citizens;
        public Country OwnCountry;
        public Planet OwnPlanet
        {
            get { return OwnCountry.OwnPlanet; }
        }

        public CityGovernment Government;

        public SmartList<CityItem> CityItems;
        public SmartList<PlanetMapCell> BelongingCells; 

        public SmartDictionary<Corporation, CityOffice> CityOffices;
        public SmartDictionary<ProductType, Exchange> Exchanges;

        public City(Country ownCountry, PlanetMapCell mapCell) : base(mapCell)
        {
            OwnCountry = ownCountry;
            OwnCountry.Cities.Add(this);
            Government = new CityGovernment(this);
            Citizens = new Citizens(this);
            CityOffices = new SmartDictionary<Corporation, CityOffice>();

            BelongingCells = new SmartList<PlanetMapCell>();
            CityItems = new SmartList<CityItem>();
            
            mapCell.BelongToCity(this);
            
            Exchanges = new SmartDictionary<ProductType, Exchange>();
            foreach (var val in Enum.GetValues(typeof (ProductType)))
            {
                var prod = (ProductType)val;
                Exchanges.Add(prod, new Exchange(prod));
            }
        }

        public CityOffice GetCityOffice(Corporation corporation)
        {
            CityOffice result;
            CityOffices.TryGetValue(corporation, out result);
            return result;
        }

        public override void StartNextTurn()
        {
            Citizens.Morning();
            foreach (var cityOffice in CityOffices)
                cityOffice.Value.Morning();
            base.StartNextTurn();
        }
        public override void EndNextTurn()
        {
            Citizens.Evening();
            foreach (var cityOffice in CityOffices)
                cityOffice.Value.Evening();
            base.EndNextTurn();
        }
    }
}
